﻿using OnlineAuction.DataAccess;
using OnlineAuction.Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace OnlineAuction.Business
{
    public class ItemBO
    {
        public bool AddItems(ItemsE item, AuctionDTO auction)
        {
            //Add new Items
            ItemDA da = new ItemDA();
            return da.AddItems(item, auction);
        }

        public List<ItemsE> ItemsSelling(string userName)
        {
            //Get all items selling
            ItemDA da = new ItemDA();
            List<ItemsE> list = new List<ItemsE>();
            DataSet ds = da.ItemsSelling(userName);
            //Convert DataRow to List items
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                list.Add(new ItemsE(int.Parse(row["ItemID"].ToString()), row["ItemName"].ToString(),
                    row["AuctionTypeName"].ToString(), int.Parse(row["AuctionID"].ToString()),
                   double.Parse(row["ExpectationPrice"].ToString()),
                   double.Parse(row["Price"].ToString()), row["TimeFinishAuction"].ToString()));
            }
            return list;
        }

        public List<ItemsE> ItemsDisabled(string userName)
        {
            //Get all items selling
            ItemDA da = new ItemDA();
            List<ItemsE> list = new List<ItemsE>();
            DataSet ds = da.ItemsDisabled(userName);
            //Convert DataRow to List items
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                list.Add(new ItemsE(int.Parse(row["ItemID"].ToString()), row["ItemName"].ToString(),
                    row["AuctionTypeName"].ToString(), int.Parse(row["AuctionID"].ToString()),
                    double.Parse(row["ExpectationPrice"].ToString()),
                    double.Parse(row["Price"].ToString()), row["TimeFinishAuction"].ToString(),
                    row["Winner"].ToString()));
            }
            return list;
        }

        public ItemsE DetailItemSelling(int auctionID)
        {
            //Get detail item selling
            ItemDA da = new ItemDA();
            DataRow row = da.DetailItemSelling(auctionID).Tables[0].Rows[0];
            string s = row["ExpectationPrice"].ToString();
            double expectationPrice = double.Parse(s.CompareTo("") == 0 ? "0" : s);
            ItemsE item = new ItemsE(int.Parse(row["CategoryID"].ToString()),
                row["ItemName"].ToString(),
                row["ItemImage1"] != null ? row["ItemImage1"].ToString() : "",
                row["ItemImage2"] != null ? row["ItemImage2"].ToString() : "",
                row["ItemImage3"] != null ? row["ItemImage3"].ToString() : "",
                row["ItemImage4"] != null ? row["ItemImage4"].ToString() : "",
                row["ItemDescription"].ToString(),
                int.Parse(row["AuctionTypeID"].ToString()),
                double.Parse(row["FirstPrice"].ToString()), expectationPrice,
                row["TimeFinishAuction"].ToString(),
                double.Parse(row["BidIncrement"] != null ? row["BidIncrement"].ToString() : "0"),
                int.Parse(row["TimeIncrement"].ToString()));
            return item;
        }

        public bool UpdateItems(ItemsE item)
        {
            //Update Items
            ItemDA da = new ItemDA();
            return da.UpdateItems(item);
        }

        public List<AuctionDTO> Search(string itemName, int categoryID)
        {
            List<AuctionDTO> list = new List<AuctionDTO>();
            ItemDA da = new ItemDA();
            DataSet ds = da.Search(itemName, categoryID);
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                string[] time = row["LeftHour"].ToString().Split(':');

                list.Add(new AuctionDTO(int.Parse(row["AuctionID"].ToString()),
                    row["ItemName"].ToString(), row["ItemImage1"].ToString(),
                    double.Parse(row["Price"].ToString()), row["LeftDay"].ToString(),
                    time[0], time[1], time[2]));
            }
            return list;
        }

        public bool DeleteItem(string ItemID)
        {
            ItemDA da = new ItemDA();
            return da.DeleteItem(ItemID);
        }
    }
}
